﻿#include "dialog.h"
#include "ui_dialog.h"

Dialog::Dialog(QListWidget *pixList, QWidget *parent) :
    QDialog(parent),
    ui(new Ui::Dialog)
{
    ui->setupUi(this);

    result[0][0] = 0;
    result[0][1] = 0;
    result[1][0] = 0;
    result[1][1] = 0;

    this->resize(547,509);
    this->setFixedSize(this->size());
    ui->stackedWidget->setCurrentWidget(ui->page);

    ui->label->setPixmap(QPixmap(":/imgs/all.jpg").scaledToHeight(ui->page_3->height()));

    ui->listWidget->setIconSize(QSize(50,50));
    ui->listWidget->setMouseTracking(true);
    ui->listWidget->setDragEnabled(true);



//    ui->lab1->setAcceptDrops(true);
//    ui->lab2->setAcceptDrops(true);
//    ui->lab3->setAcceptDrops(true);
//    ui->lab4->setAcceptDrops(true);

//    ui->lab1->setMouseTracking(true);
//    ui->lab2->setMouseTracking(true);
//    ui->lab3->setMouseTracking(true);
//    ui->lab4->setMouseTracking(true);


    ui->lab1->move(ui->groupBox_2->mapToParent(this->rect().topLeft()));
    ui->lab2->move(ui->groupBox_3->mapToParent(this->rect().topLeft()));
    ui->lab3->move(ui->groupBox_4->mapToParent(this->rect().topLeft()));
    ui->lab4->move(ui->groupBox_5->mapToParent(this->rect().topLeft()));




    for(int i = 0;i < pixList->count();++i){
        ui->listWidget->addItem("");
        ui->listWidget->item(i)->setIcon(pixList->item(i)->icon());
        ui->listWidget->item(i)->setToolTip(pixList->item(i)->toolTip());

        ui->listWidget->item(i)->setFlags(Qt::ItemIsSelectable | Qt::ItemIsEnabled );

    }

    connect(ui->listWidget,SIGNAL(drop(int,QPoint)),this,SLOT(onDrop(int,QPoint)));
}

Dialog::~Dialog()
{
    delete ui;
}

void Dialog::onDrop(int index, QPoint pos)
{

    QString path = QString(":/imgs/%1.png").arg(index);
    QRect rect1(30,30,180,180);
    QRect rect2(250,30,180,180);
    QRect rect3(30,265,180,180);
    QRect rect4(260,260,180,180);
    if(rect1.contains(pos)){
        ui->lab1->setPixmap(QPixmap(path).scaled(ui->lab1->width(),ui->lab1->height()));
        result[0][0] = index;
    }else if(rect2.contains(pos)){
        ui->lab2->setPixmap(QPixmap(path).scaled(ui->lab2->width(),ui->lab2->height()));
        result[0][1] = index;
    }else if(rect3.contains(pos)){
        ui->lab3->setPixmap(QPixmap(path).scaled(ui->lab3->width(),ui->lab3->height()));
        result[1][0] = index;
    }
    else if(rect4.contains(pos)){
        ui->lab4->setPixmap(QPixmap(path).scaled(ui->lab4->width(),ui->lab4->height()));
        result[1][1] = index;
    }

    if(result[0][0] == 1 && result[0][1] == 2 && result[1][0] == 3 && result[1][1] == 4){
        QMessageBox::information(0,"提示","恭喜您完成了拼图!");
        ui->stackedWidget->setCurrentWidget(ui->page_3);
    }
}
